//
//  RecommendHeaderView.m
//  AutoHome
//
//  Created by wqh on 15-5-10.
//  Copyright (c) 2015年 Wqh. All rights reserved.
//

#import "RecommendHeaderView.h"
#import "AFNetworking.h"
#import "RecommendModel.h"
#import "UIButton+WebCache.h"

@interface RecommendHeaderView()<UIScrollViewDelegate>
{
    AFHTTPRequestOperationManager *_afManager;
    NSMutableArray *_dataArr;
    NSMutableArray *_picArr;
    NSString *_url;
    
    UIScrollView *_scrollView;
    UIPageControl *_pageControl;
}
@end

@implementation RecommendHeaderView

- (instancetype)initWithFrame:(CGRect)frame Url:(NSString *)url {
    if (self = [super initWithFrame:frame]) {
        _url = url;
        [self downloadData];
    }
    return self;
}

- (void)downloadData {
    
    _dataArr = [NSMutableArray array];
    
    _afManager = [AFHTTPRequestOperationManager manager];
    _afManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [_afManager GET:_url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self downloadDataSuccess:responseObject];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];
}

- (void)downloadDataSuccess:(id)responseObject {
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
    for (NSDictionary *recDict in dict[@"result"][@"focusimg"]) {
        RecommendModel *model = [[RecommendModel alloc] init];
        [model setValuesForKeysWithDictionary:recDict];
        [_dataArr addObject:model];
    }
    [self loadPic];
}

- (void)loadPic {
    
    _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    _picArr = [NSMutableArray array];
    
    NSString *headerPicPath = nil;
    NSInteger count = _dataArr.count;
    for (NSInteger index = 0; index < _dataArr.count+2; index++) {
        if (index == 0) {
            headerPicPath = [_dataArr[count-1] imgurl];
        }else if(index == count+1) {
            headerPicPath = [_dataArr[0] imgurl];
        }else {
            headerPicPath = [_dataArr[index -1] imgurl];
        }
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(kScreenWidth*index, 0, kScreenWidth, self.bounds.size.height);
        [button sd_setBackgroundImageWithURL:[NSURL URLWithString:headerPicPath] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"black_default_640_320"]];
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [_scrollView addSubview:button];
    }
    _scrollView.contentSize = CGSizeMake(kScreenWidth*(_dataArr.count+2), self.bounds.size.height);
    _scrollView.contentOffset = CGPointMake(kScreenWidth, 0);
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.delegate = self;
    
    [self addSubview:_scrollView];
    
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.bounds.size.height-20, kScreenWidth, 20)];
    _pageControl.numberOfPages = _dataArr.count;
    [_pageControl addTarget:self action:@selector(pageClick:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_pageControl];
}

- (void)pageClick:(UIPageControl *)pageControl {
    
}

- (void)btnClick:(UIButton *)button {
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGPoint curPoint = scrollView.contentOffset;
    if (curPoint.x == 0) {//0 表示假的最后一张
        //修改成真正的最后一张的内容偏移量
        scrollView.contentOffset = CGPointMake((_dataArr.count)*_scrollView.bounds.size.width, 0);
    }else if (curPoint.x == (_dataArr.count+1)*_scrollView.bounds.size.width){
        //滚动到真正的第一张
        scrollView.contentOffset = CGPointMake(_scrollView.bounds.size.width, 0);
    }
    _pageControl.currentPage = (_scrollView.contentOffset.x-kScreenWidth)/kScreenWidth;
}



@end
